const service = require('../service/articleService')

const jwt = require('../util/jwt')
const deFile = require('../util/delFile.js')
const controller = {
  // 添加标签
  async addLabel (req, res) {
    const { labelName } = req.body
    const data = await service.addLabel(labelName)
    if (data.createTime) {
      res.send({ ok: 1, info: '添加完成', data })
    } else {
      res.send({ ok: 0, info: '添加失败' })
    }
  },
  // 查找标签
  async findLabel (req, res) {
    let resPromise
    let data = await service.findLabel()
    if (data.length) {
      data = data.map(async (item) => {
        // 去查找标签对应的片数
        let r = await service.findArticleSingle(item.labelName)
        return {
          articleCount: r.length,
          createTime: item.createTime,
          labelName: item.labelName,
          _id: item._id,
        }
      })
      resPromise = await Promise.all(data)
    }
    res.send({
      ok: 1,
      stateCode: 200,
      info: resPromise || [],
      count: data.length,
    })
  },
  // 删除标签
  async delLabel (req, res) {
    let { id, token } = req.query
    username = jwt.verify(token)?.uname
    if (username == 'admin') {
      // 删除文件夹中文件
      let data = await service.delLabel(id)
      res.send({
        ok: data.acknowledged ? 1 : 0,
        code: 200,
        info: data.acknowledged ? '删除成功' : '删除失败',
      })
    } else {
      res.send({ ok: 0, code: 401, info: '你不是超级管理员哦！！！' })
    }
  },
  // 上传图片
  async updatedImage (req, res) {
    if (!req.file) return res.send({ ok: 0, info: '上传失败' })
    const image = `ArticleUploads/${req.file.filename}`
    res.send({ ok: 1, imageUrl: image })
  },
  // 删除上传来的图片
  async delImage (req, res) {
    const { delImages } = req.body
    delImages.forEach((item) => {
      deFile(item.split('ArticleUploads/')[1], 'ArticleUploads')
    })
    res.send({ ok: 1 })
  },
  // 上传文章
  async addArticle (req, res) {
    let { title, info, articleType, bg } = req.body
    bg = bg ? bg : 'images/article.jpeg'
    let data = await service.addArticle(title, info, articleType, bg)
    if (data.createTime) {
      res.send({ ok: 1, info: '上传完成' })
    } else {
      res.send({ ok: 0, info: '上传失败' })
    }
  },
  // 修改文章
  async updateArticle (req, res) {
    let { _id, bg } = req.body
    bg = bg ? bg : 'images/article.jpeg'
    let data = await service.updateArticle(_id, req.body, bg)

    if (data.acknowledged) {
      res.send({ ok: 1, info: '修改成功' })
    } else {
      res.send({ ok: 0, info: '修改失败' })
    }
  },
  // 查找前台全部文章
  async findArticle (req, res) {
    let { page, limite, where } = req.query
    page = page ? page : '1'
    limite = limite ? limite : '7'
    where = where ? JSON.parse(where) : {}
    where = { ...where, title: new RegExp(where.title || '.') }
    let data = await service.getAllList(page, limite, where)
    res.send({ ok: 1, info: data })
  },
  // 查找后台全部文章
  async findArticleendList (req, res) {
    let { page, limite, where } = req.query
    where = where ? JSON.parse(where) : {}
    where = { ...where, title: new RegExp(where.title || '.') }
    let data = await service
      .findArticleendList(page, limite, where)
      .catch((err) => err)
    res.send({ ok: 1, info: data })
  },
  // 查找单个文章
  async findArticleSingle1 (req, res) {
    let { id } = req.query
    if (!id) return
    let data = await service.findArticleSingle1(id)
    let articleNews = await service.getAllList(1, 2, {})
    res.send({ ok: 1, info: data, articleNews })
  },
  // 删除文章
  async delArticle (req, res) {
    let { id, token, images } = req.body
    username = jwt.verify(token)?.uname
    if (username == 'admin') {
      // 删除文件夹中文件
      images?.length &&
        images.forEach((item) => {
          deFile(item, 'ArticleUploads')
        })
      let data = await service.delArticle(id)
      res.send({
        ok: data.acknowledged ? 1 : 0,
        code: 200,
        info: data.acknowledged ? '删除成功' : '删除失败',
      })
    } else {
      res.send({ ok: 0, code: 401, info: '你不是超级管理员哦！！！' })
    }
  },

}
module.exports = controller
